package com.samuel.blog.interceptor;

import com.samuel.blog.constant.CommonConstant;
import com.samuel.blog.entity.AuthorEntity;
import com.samuel.blog.entity.JurisdictionEntity;
import com.samuel.blog.entity.RoleEntity;
import com.samuel.blog.service.AuthorService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 权限拦截器
 * @author 开机
 * @since 2018-01-25
 */
public class JurInterceptor extends HandlerInterceptorAdapter {

	@Resource
	private AuthorService authorService;
	/**
	 * 访问controller之前
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// localhost:8090/blog-controller
		String contextPath = request.getContextPath();
		String requestUri = request.getRequestURI();
		
	    String privUrl = requestUri.substring(contextPath.length());
		
		AuthorEntity author = (AuthorEntity)request.getSession().getAttribute(CommonConstant.USER_LOGIN_SESSION_KEY);
		
		RoleEntity roleEntity = authorService.selectRoleByRedis(author.getAuthorId());
		List<JurisdictionEntity> list = authorService.selectJurByRedis(author.getAuthorId());
		
		if (null == roleEntity) {
			response.sendRedirect(contextPath+"/admin/home/message.html");
			return false;
		}
		if (CommonConstant.ADMIN_ROLE_ID.equals(roleEntity.getRoleId())) {
			return true;
		}
		if (null == list || list.size() <= 0 || list.get(0) == null) {
			response.sendRedirect(contextPath+"/admin/home/message.html");
			return false;
		}
		
		if (checkPrivUrl(privUrl, list)) {
			return true;
		} else {
			response.sendRedirect(contextPath+"/admin/home/message.html");
			return false;
		}
	}
	
	/**
	 * @param privUrl 请求的URL
	 * @param list    自己的权限URL
	 * @return 检测权限
	 */
	private boolean checkPrivUrl(String privUrl, List<JurisdictionEntity> list) {
		for (JurisdictionEntity jur : list) {
			if (privUrl.equals(jur.getUrl())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 访问controller之后 但是在返回页面之前
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
						   ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 整个请求处理完毕回调方法，即在视图渲染完毕时回调
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	/**
	 * 处理异步请求
	 *  过程  preHandle - afterConcurrentHandlingStarted - preHandle - postHandle - afterCompletion
	 */
	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

}
